package com.dgz.springboot18.service.impl;

import com.dgz.springboot18.mapper.StudentMapper;
import com.dgz.springboot18.mapper.StudentTaskMapper;
import com.dgz.springboot18.mapper.TaskMapper;
import com.dgz.springboot18.pojo.StudentInfo;
import com.dgz.springboot18.pojo.StudentTask;
import com.dgz.springboot18.pojo.TaskInfo;
import com.dgz.springboot18.pojo.vo.StuAndTask;
import com.dgz.springboot18.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private StudentTaskMapper studentTaskMapper;

    /**
     * 添加学生
     * @param studentInfo
     */
    @Override
    public void addStudent(StudentInfo studentInfo){
        studentMapper.save(studentInfo);
    }

    /**
     * 根据学生名称和密码查找学生
     * @param studentInfo
     * @return
     */
    @Override
    public StudentInfo queryStu(StudentInfo studentInfo){
        Optional<StudentInfo> result = studentMapper.findOne(Example.of(studentInfo));
        return result.isPresent()?result.get():null;
    }

    @Override
    public List<StuAndTask> querySignTask(String name){
        List<StuAndTask> stuAndTaskResult=new ArrayList<>();

        List<TaskInfo> taskResult = taskMapper.findAll();  //查询出所有的任务
        List<StudentInfo> studentInfos=new ArrayList<>();   //查询出所有任务的当前学生信息
        List<StudentTask> StudentTasks=new ArrayList<>();  //查询出所有是否打卡信息
        for (TaskInfo taskInfo : taskResult) {
            StudentTask studentTask=new StudentTask();
            studentTask.setTaskId(taskInfo.getTaskId());
            List<StudentTask> list= studentTaskMapper.findAll(Example.of(studentTask));//根据任务查询出所有任务和学生的连接
            for (StudentTask task : list) {
                StudentInfo studentInfo=new StudentInfo();
                studentInfo.setStuName(name);
                studentInfo.setStuId(task.getStuId());
                List<StudentInfo> all = studentMapper.findAll(Example.of(studentInfo));
                if (all.size()!=0){
                    studentInfos.add(all.get(0));
                    StudentTasks.add(new StudentTask(all.get(0).getStuId(),task.getTaskId(),task.getFlagTask(),null));
                }
            }
        }
        for (int i=0;i<taskResult.size();i++){
            StuAndTask stuAndTask=new StuAndTask();
            stuAndTask.setTaskId(taskResult.get(i).getTaskId());
            stuAndTask.setStuId(studentInfos.get(i).getStuId());
            stuAndTask.setStuName(studentInfos.get(i).getStuName());
            stuAndTask.setTaskName(taskResult.get(i).getTaskName());
            stuAndTask.setTaskContent(taskResult.get(i).getTaskContent());
            stuAndTask.setTaskAdmin(taskResult.get(i).getTaskAdmin());
            stuAndTask.setTaskAndTime(taskResult.get(i).getTaskAndTime());
            stuAndTask.setFlagTask(StudentTasks.get(i).getFlagTask());
            stuAndTaskResult.add(stuAndTask);
        }
        return stuAndTaskResult;
    }


}
